package com.lanyou.esb.web.quantityControlcontrol;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.userManagement.User;
import com.lanyou.cook.service.security.UserSessionService;
import com.lanyou.cook.util.gson.GsonUtil;
import com.lanyou.esb.common.CommonUtils;
import com.lanyou.esb.repository.HttpRepository;
import com.lanyou.esb.repository.QuantityControlRepository;
import com.lanyou.esb.service.AccessLogService;
import com.lanyou.esb.service.QuantityControlService;
import com.lanyou.esb.web.param.HttpInterfaceParam;
import com.lanyou.esb.web.param.InterfaceRequestQuantityParam;
import com.lanyou.esb.web.quantityControlcontrol.vo.QuantityControlVo;

/**
 * 流量控制控制器
 * 
 * @author Davey.wu
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/flowControl")
public class QuantityControlController {
	@Autowired
	QuantityControlService quantityControlService;

	@Autowired
	AccessLogService serviceLogSearchService;

	@Autowired
	UserSessionService userSessionService;

	@Autowired
	HttpRepository httpRepository;

	@Autowired
	QuantityControlRepository quantityControlRepository;

	/**
	 * 获取流量控制
	 * 
	 * @param param
	 *            ：查询参数
	 * @param pageable
	 *            ：分页对象
	 * @return
	 */
	@RequestMapping("/search/findRequestQuantity")
	public Page<QuantityControlVo> findRequestQuantity(
			HttpInterfaceParam param, Pageable pageable) {
		if (StringUtils.isNotBlank(param.getName())) {
			param.setName("%" + param.getName() + "%");
		}
		User user = userSessionService.getCurrentAuthenticatedUser();
		// 查询数据
		return quantityControlService.findRequestQuantity(param, pageable,
				user.getId());

	}

	/**
	 * 更新流量控制信息
	 * 
	 * @param param
	 *            ：查询参数
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	@RequestMapping(value = "/updateRequestQuantity", produces = "application/json", method = RequestMethod.POST)
	public String updateRequestQuantity(
			@RequestBody InterfaceRequestQuantityParam param)
			throws HttpException, IOException, URISyntaxException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			// 更新流量
			quantityControlService.updateQuantity(param);
			result.put(CommonUtils.MSG_FIELD_RESULT, true);
		} catch (Exception e) {
			result.put(CommonUtils.MSG_FIELD_RESULT, false);
			result.put(CommonUtils.MSG_FIELD_MSG, e.getMessage());
		}
		return GsonUtil.getDefaultGson().toJson(result);

	}

}
